<?php

namespace App\Http\Controllers\Admin;
use DB;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
use App\Http\Requests;

class ComtableController extends CommonController
{
    
    public function index()
    {       //获取数据表 commodity   comimg  comtable  的 数据
            //leftjoin  左连接    
    	$list = DB::table("commodity")
                ->where("commodity.comstatus",">","0")    
                ->leftjoin("comimg","comimg.sid" ,"=", "commodity.id")
                ->leftjoin("comtable","comtable.sid","=","commodity.id")
                ->paginate(5);
          
         
        return view("admin.comtable.index",['list'=>$list]);
       		

    }
    //加载添加表单
    public function create()
    {
    	return view("admin.comtable.add");
    }
    //执行信息添加
    public function store(Request $request)
    {	
		if(!empty($request->comname) && !empty($request->comprice) && !empty($request->comstatus) && !empty($request->name) && !empty($request->comcofig) && !empty($request->comcolor) && !empty($request->inventory) && !empty($request->comdesc) ){
        //在commodity 数据表中添加数据
		$date = ["comname"=>$request->comname,"comprice"=>$request->comprice,"stime"=>date("Y-m-d H:i:s",time()),"comstatus"=>$request->comstatus];
           
		$dd = \DB::table("commodity")->insertGetId($date);
		
		if($dd>0){
			//在comtable 表中添加数据
            $table = ["comtaby"=>$request->name,"comcofig"=>$request->comcofig,"comcolor"=>$request->comcolor,"inventory"=>$request->inventory,"comdesc"=>$request->comdesc,'sid'=>$dd];
        	
            $ta = \DB::table("comtable")->insert($table);
        	
        	if($ta>0){
        		//执行图片上传
        		if($request->hasFile('fimg')) {
		            //获取上文件信息
		    	// var_dump($_FILES);
		            $file = $request->file('fimg');
		            //获取上传文件的后缀名
		            $ext = $file->getClientOriginalExtension();
		            //随机一个新的文件名
		            $filename = time().rand(1000,9999).".".$ext;
		            //执行文件移动
		            $newfile = $file->move("./uploads/",$filename);
		    		//执行图片缩放
		    		$zimg = Image::make("./uploads/".$filename)->resize(100,100);
		    		$zimg->save("./uploads/s_".$filename);

		    		$fimg = Image::make("./uploads/".$filename)->resize(50,50);
		    		$fimg->save("./uploads/".$filename);
		    		//在comimg表中添加数据
		    		$fimg = ["sid"=>$dd,"fimg"=>$filename,"zimg"=>"s_".$filename];

		    		$img = \DB::table("comimg")->insertGetId($fimg);
		    		if($img>0){
                         $info = ("商品添加成功");
		    		}else{
		    			  $info = ("商品添加添加失败");
		    		}   
                    
		    	}else{
                    $info = ("图片信息不能为空");
                }	
        	//	
        	}else{
        		   $info =("图片添加失败");
        	}//

        }else{
            $info = ("商品信息不能为空！");
        }
    }else{
        $info = ("请填写完整信息");
    }
       
    return view("admin.comtable.info" ,['info'=>$info]);
    }
    //加载编辑表单
    public function edit($id)
    {
        //获取3个数据表里的数据  加载到修改页面
         $stu = DB::table("commodity")
                ->where("commodity.id","=",$id )
                ->leftjoin("comimg","commodity.id","=","comimg.sid" )
                ->leftjoin("comtable","commodity.id","=","comtable.sid")
                ->get();
          
          
        return view("admin.comtable.update",['list'=>$stu]);   
    }
    //执行信息修改
    public function update(Request $request,$id)
    {   
        //判断是否有图片传入进来  如果有则跳过 传入图片
       if(!empty($request->fimg)){
        //为了执行 修改图片时 同时删掉文件夹里的图片
        $stu = DB::table("commodity")
            ->where("commodity.id","=",$id)
            ->leftjoin("comimg","commodity.id","=","comimg.sid" )
            ->leftjoin("comtable","commodity.id","=","comtable.sid")
            ->get();

            foreach ($stu as $v) {
                 @unlink("./uploads/".$v->fimg);
                 @unlink("./uploads/".$v->zimg);
            }
        //在commodity 数据表中修改数据
        $date = ["comname"=>$request->comname,"comprice"=>$request->comprice,"stime"=>time(),"comstatus"=>$request->comstatus];

        $dd = \DB::table("commodity")->where("id",$id)->update($date);
        
        if($dd>0){
        //在comtable 表中修改数据
        $table = ["comcofig"=>$request->comcofig,"comcolor"=>$request->comcolor,"inventory"=>$request->inventory,"comdesc"=>$request->comdesc,'sid'=>$id];
        \DB::table("comtable")->where("sid",$id)->update($table);
        
            //执行图片上传
            if($request->hasFile('fimg')) {
            //获取上文件信息
            // var_dump($_FILES);
            $file = $request->file('fimg');
            //获取上传文件的后缀名
            $ext = $file->getClientOriginalExtension();
            //随机一个新的文件名
            $filename = time().rand(1000,9999).".".$ext;
            //执行文件移动
            $newfile = $file->move("./uploads/",$filename);
            //执行图片缩放
            $zimg = Image::make("./uploads/".$filename)->resize(100,100);
            $zimg->save("./uploads/s_".$filename);

            $fimg = Image::make("./uploads/".$filename)->resize(50,50);
            $fimg->save("./uploads/".$filename);
            //在comimg表中添加数据
            $fimg = ["sid"=>$id,"fimg"=>$filename,"zimg"=>"s_".$filename];

            $img = \DB::table("comimg")->where("sid",$id)->update($fimg);
            if($img>0){
                //获取3个数据表里的信息   发送到index主页
                $stu = DB::table("commodity")
                ->where("commodity.comstatus","!=","0")
                ->leftjoin("comimg","commodity.id","=","comimg.sid" )
                ->leftjoin("comtable","commodity.id","=","comtable.sid")
                ->paginate(5);

                 return view("admin.comtable.index",['list'=>$stu]);
                
                
            }else{
                    $info = ("修改失败");
                }
          }
        
        }else{
             $info =("修改失败");
            }

      }else{
        

        //在commodity 数据表中修改数据
        $date = ["comname"=>$request->comname,"comprice"=>$request->comprice,"stime"=>time(),"comstatus"=>$request->comstatus];

        $dd = \DB::table("commodity")->where("id",$id)->update($date);
        
        if($dd>0){
        //在comtable 表中修改数据
        $table = ["comcofig"=>$request->comcofig,"comcolor"=>$request->comcolor,"inventory"=>$request->inventory,"comdesc"=>$request->comdesc,'sid'=>$id];
        \DB::table("comtable")->where("sid",$id)->update($table);

         $stu = DB::table("commodity")
            ->where("commodity.comstatus",">","0")
            ->leftjoin("comimg","commodity.id","=","comimg.sid" )
            ->leftjoin("comtable","commodity.id","=","comtable.sid")
            ->paginate(5);   

        return view("admin.comtable.index",['list'=>$stu]);

        }      
     }
    }       
    //执行删除
    public function destroy($id)
    {
        $stu = DB::table("commodity")
            ->where("commodity.id","=",$id)
            ->leftjoin("comimg","commodity.id","=","comimg.sid" )
            ->leftjoin("comtable","commodity.id","=","comtable.sid")
            ->get();

            foreach ($stu as $v) {
                 @unlink("./uploads/".$v->fimg);
                 @unlink("./uploads/".$v->zimg);
            }

        DB::table("commodity")->delete($id);
        DB::table("comtable")->where("sid",$id)->delete();
        DB::table("comimg")->where("sid",$id)->delete();
        //为了执行 修改图片时 同时删掉文件夹里的图片
        
        return  back("admin/comtable");
    }


}
